---
# Copyright 2017, Rackspace US, Inc.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# This is a generic task set which can be used to execute
# a service action on target hosts for any services. This
# is useful for executing a SIGHUP (reload) to load up any
# configuration changes or to restart services as required.
#
# Inputs:
# - service_name: Any service found matching this prefix will be acted on.
# - service_action: The action to execute [stop, start, restart, reload].

- name: Gather service list
  shell: "systemctl list-unit-files --state=enabled --type=service | awk '/{{ service_name }}.* enabled$/ {print $1}'"
  args:
    executable: "/bin/bash"
  register: _enabled_services
  changed_when: false
  tags:
    - skip_ansible_lint

- name: Execute service action
  service:
    name: "{{ service_file }}"
    state: "{{ service_action }}"
  with_items: "{{ (_enabled_services.stdout_lines | difference(service_negate | default([]))) | list }}"
  loop_control:
    loop_var: service_file

- name: Disable the service restart requirement
  ini_file:
    dest: "/etc/ansible/facts.d/openstack_ansible.fact"
    section: "{{ service_fact | default(service_name) }}"
    option: need_service_restart
    value: False
